package com.wzdigit.wms.wms.controller.basic;

import com.wzdigit.framework.exception.RestStatusException;
import com.wzdigit.framework.model.response.ErrorEntity;
import com.wzdigit.wms.common.utils.ValidatorUtil;
import com.wzdigit.wms.basic.client.Pager;
import com.wzdigit.wms.basic.client.PagerInfo;
import com.wzdigit.wms.basic.client.request.basic.*;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.basic.client.response.basic.ImportWorkDivisionDtlResponse;
import com.wzdigit.wms.basic.client.response.basic.ImportWorkDivisionResponse;
import com.wzdigit.wms.basic.client.response.basic.WorkDivisionDtlResponse;
import com.wzdigit.wms.basic.client.response.basic.WorkDivisionResponse;
import com.wzdigit.wms.wms.constant.WmsWareConstant;
import com.wzdigit.wms.wms.service.basic.WorkDivisionService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.rowset.serial.SerialException;
import javax.validation.Valid;
import java.sql.SQLException;
import java.util.List;

/**
 * 工作分工 前端控制器
 * @author yangdongjie
 * @LocationController.java
 * @2020年12月31日下午5:56:18
 */
@RestController
@Api(tags = "工作分工")
@RequestMapping(value = WmsWareConstant.API_VERSION)
public class WorkDivisionController {
	
	@Autowired
    private WorkDivisionService workDivisionService;

    private static final String REQUEST_MODEL = "/workdivision";
    
    /**
     * 创建工作分工
     * @param request
     * @param bindingResult
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "创建工作分工",notes = "创建工作分工")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PostMapping(value = REQUEST_MODEL + "/createWorkDivision")
    public <T> Result<T> createWorkDivision(
    		@RequestBody @Valid CreateWorkDivisionRequest request, 
    		BindingResult bindingResult, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
        	ValidatorUtil.validator(bindingResult);
            return workDivisionService.createWorkDivision(request, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    /**
     * 修改工作分工
     * @param request
     * @param bindingResult
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "修改工作分工",notes = "修改工作分工")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PutMapping(value = REQUEST_MODEL + "/updateWorkDivision")
    public <T> Result<T> updateWorkDivision(
    		@RequestBody @Valid UpdateWorkDivisionRequest request, 
    		BindingResult bindingResult, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
        	ValidatorUtil.validator(bindingResult);
            return workDivisionService.updateWorkDivision(request, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    /**
     * 启用/禁用 工作分工
     * @param request
     * @param bindingResult
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "启用/禁用 工作分工",notes = "启用/禁用 工作分工")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PutMapping(value = REQUEST_MODEL + "/updateWorkDivisionIsEnabled")
    public <T> Result<T> updateWorkDivisionIsEnabled(
    		@RequestBody @Valid UpdateWorkDivisionIsEnabledRequest request, 
    		BindingResult bindingResult, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
        	ValidatorUtil.validator(bindingResult);
            return workDivisionService.updateWorkDivisionIsEnabled(request, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
	/**
	 * 查询工作分工
	 * @param workDivisionCode
	 * @param workDivisionType
	 * @param pager
	 * @param servletRequest
	 * @param servletResponse
	 * @return
	 */
	@ApiOperation(value = "查询工作分工",notes = "查询工作分工")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/searchWorkDivision")
    public <T> PagerInfo<List<WorkDivisionResponse>> searchWorkDivision(
    		@ApiParam(value = "分工代码", required = false) 
    		@RequestParam(required = false) String workDivisionCode, 
    		@ApiParam(value = "分工类别：1：仓管员分工，2：质检员分工", required = false) 
    		@RequestParam(required = false) Integer workDivisionType, 
    		@ApiParam(value = "是否启用", required = false) 
    		@RequestParam(required = false) Integer isEnabled, 
    		@ApiParam(value = "分工代码模糊查询", required = false) 
    		@RequestParam(required = false) String filter, 
    		Pager<T> pager,
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
            return workDivisionService.searchWorkDivision(workDivisionCode, 
            		workDivisionType, isEnabled, filter, pager);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
	
	
	/**
	 * 创建工作分工明细
	 * @param request
	 * @param bindingResult
	 * @param servletRequest
	 * @param servletResponse
	 * @return
	 * @throws SQLException 
	 * @throws SerialException 
	 */
	@ApiOperation(value = "创建工作分工明细",notes = "创建工作分工明细")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PostMapping(value = REQUEST_MODEL + "/createWorkDivisionDtl")
    public <T> Result<T> createWorkDivisionDtl(
    		@RequestBody @Valid CreateWorkDivisionDtlRequest request, 
    		BindingResult bindingResult, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) throws SerialException, SQLException {
        try {
        	ValidatorUtil.validator(bindingResult);
            return workDivisionService.createWorkDivisionDtl(request, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
	
	/**
	 * 修改工作分工明细
	 * @param request
	 * @param bindingResult
	 * @param servletRequest
	 * @param servletResponse
	 * @return
	 */
	@ApiOperation(value = "修改工作分工明细",notes = "修改工作分工明细")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PutMapping(value = REQUEST_MODEL + "/updateWorkDivisionDtl")
    public <T> Result<T> updateWorkDivisionDtl(
    		@RequestBody @Valid UpdateWorkDivisionDtlRequest request, 
    		BindingResult bindingResult, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
        	ValidatorUtil.validator(bindingResult);
            return workDivisionService.updateWorkDivisionDtl(request, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
	
	/**
     * 删除工作分工明细
     * @param workDivisionDtlIds
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "删除工作分工明细",notes = "删除工作分工明细")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @DeleteMapping(value = REQUEST_MODEL + "/deleteWorkDivisionDtl")
    public <T> Result<T> deleteWorkDivisionDtl(
    		@ApiParam(value = "分工明细ID", required = true) 
    		@RequestParam(required = true) Integer workDivisionDtlId, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
            return workDivisionService.deleteWorkDivisionDtl(workDivisionDtlId, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    /**
     * 查询工作分工明细
     * @param workDivisionId
     * @param pager
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "查询工作分工明细",notes = "查询工作分工明细")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/searchWorkDivisionDtl")
    public <T> PagerInfo<List<WorkDivisionDtlResponse>> searchWorkDivisionDtl(
    		@ApiParam(value = "分工ID", required = true) 
    		@RequestParam Integer workDivisionId, 
    		Pager<T> pager,
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
            return workDivisionService.searchWorkDivisionDtl(workDivisionId, pager);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    
    /**
     * 导入《工作分工》
     * @param file
     * @param servletRequest
     * @param servletResponse
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "导入《工作分工》", notes = "导入《工作分工》")
    @ApiResponses({@ApiResponse(code = 500, message = "系统异常", response = ErrorEntity.class)})
    @PostMapping(value = REQUEST_MODEL + "/importWorkDivision")
    public ImportWorkDivisionResponse importWorkDivision(MultipartFile file, 
			HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws Exception {
    	try {
			return workDivisionService.importWorkDivision(file, servletRequest, servletResponse);
        } catch (RestStatusException e) {
        	e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
	}
    
    /**
     * 导入《工作分工明细》
     * @param file
     * @param servletRequest
     * @param servletResponse
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "导入《工作分工明细》", notes = "导入《工作分工明细》")
    @ApiResponses({@ApiResponse(code = 500, message = "系统异常", response = ErrorEntity.class)})
    @PostMapping(value = REQUEST_MODEL + "/importWorkDivisionDtl")
    public ImportWorkDivisionDtlResponse importWorkDivisionDtl(MultipartFile file, 
			HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws Exception {
    	try {
			return workDivisionService.importWorkDivisionDtl(file, servletRequest, servletResponse);
        } catch (RestStatusException e) {
        	e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
	}
}
